//
//  KVEmoticonCell.swift
//  KVEmoticon
//
//  Created by 李康卫 on 16/7/29.
//  Copyright © 2016年 李康卫. All rights reserved.
//

import UIKit
///一页的总表情总数
let emoticonsCountOfPage = 20
protocol KVEmoticonCellDelegate : NSObjectProtocol {
    //声明代理方法
    func emoticonCellButtonDidClick(emoticonModel: KVEmoticonModel)
    ///删除按钮被点击
    func emoticonCellDeleteBtnDidClick()
}

class KVEmoticonCell: UICollectionViewCell {
    ///btn的列数
    let emoticonCulomOfPage = 7
    ///btn的行数
    let emoticonRowOfPage = 3
    ///btn的间距
    let emoticonMargin: CGFloat = 5
    ///下部的距离
    let emoticonBottom: CGFloat = 25
    ///保存创建的btns
    var emoticonBtns = [KVEmoticonButton]()
    /// 设置代理属性
    weak var delegate: KVEmoticonCellDelegate?
    
    var emoticons: [KVEmoticonModel]? {
        didSet{
            //先隐藏btn有模型时才显示
            for button in self.emoticonBtns {
                button.hidden = true
            }
            
            //给Btn赋值
            for (index,emoticon) in self.emoticons!.enumerate() {
                // MARK : - error
                let btn = self.emoticonBtns[index]
                //给Button赋值
                btn.emoticonModel = emoticon
                btn.hidden = false
            }
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.contentView.backgroundColor = UIColor(white: 237.0/255, alpha: 1)
        //添加子控件
        addSubButtons()
    }
    override func layoutSubviews() {
        
        super.layoutSubviews()
        //设置btn的frame
        setupBtnsFrame()

    }
    // MARK: - 按钮点击事件
    @objc private func buttonDidClick(button: KVEmoticonButton) {
        //按钮被点击回调到主View让主View去处理
        self.delegate?.emoticonCellButtonDidClick(button.emoticonModel!)
       
    }
    
    @objc private func deleteBtnDidClick() {
       self.delegate?.emoticonCellDeleteBtnDidClick()
    }
    private func addSubButtons() {
        //创建20个btn
        for _ in 0..<emoticonsCountOfPage {
            let button = KVEmoticonButton()
            //添加点击事件
            button.addTarget(self, action: "buttonDidClick:", forControlEvents: UIControlEvents.TouchUpInside)
            //保存并添加
            self.emoticonBtns.append(button)
            self.contentView.addSubview(button)
            
        }
        self.contentView.addSubview(deleteBtn)

    }
    private func setupBtnsFrame() {

        let btnW = (UIScreen.mainScreen().bounds.width - emoticonMargin*2)/CGFloat(emoticonCulomOfPage)
        let btnH = (self.contentView.bounds.height - emoticonBottom)/CGFloat(emoticonRowOfPage)
        
        for (index,btn) in self.emoticonBtns.enumerate() {
            let bntX = btnW * CGFloat(index%emoticonCulomOfPage)
            let btnY = btnH * CGFloat(index/emoticonCulomOfPage)
            btn.frame = CGRect(x: bntX + emoticonMargin, y: btnY, width: btnW, height: btnH)
        }
        
        let deleteX = CGFloat(emoticonCulomOfPage - 1)*btnW
        let deleteY = CGFloat(emoticonRowOfPage - 1)*btnH
        deleteBtn.frame = CGRect(x: deleteX, y: deleteY, width: btnW, height: btnH)

    }
   
    // MARK: - 懒加载
    private lazy var deleteBtn: UIButton = {
        let deleteBtn = UIButton()
        deleteBtn.setImage(UIImage(named: "compose_emotion_delete"), forState: UIControlState.Normal)
        deleteBtn.setImage(UIImage(named: "compose_emotion_delete_highlighted"), forState: UIControlState.Selected)
        deleteBtn.addTarget(self, action: "deleteBtnDidClick", forControlEvents: UIControlEvents.TouchUpInside)
        return deleteBtn
    }()

    
}
